import Reflux from 'reflux'
import StateMixin from 'reflux-state-mixin';
import StandardAction from 'pages/serviceStandard/actions/standard'
import $ from 'jquery';
import API from 'config/api';

export default Reflux.createStore({
  mixins: [StateMixin.store],
  listenables: [StandardAction],
  getInitialState() {
    return {
      models: [],
      model: {},
      visible: false,
      visible2: false
    }
  },
  onGetStandardList() {
    $.ajax({
      url: API.GET_STANDARDLIST,
      method: 'get',
      success: (data) => {
        //log(this.state);
        this.setState({
          models: data.data,
          visible2: false
        })
      }
    })
  },
  onStandardCreate(filter) {
    $.ajax({
      url: API.CREATE_STANDARD,
      method: 'put',
      data: JSON.stringify(filter),
      contentType: 'application/json',
      datatype:'json',
    })
    .done((data) => {
      if (data.errCode === 200) {
        this.setState({
          visible: false
        })
        StandardAction.standardCreate.completed(data)
      }
    })
  },
  onDelStandard(filter) {
    $.ajax({
      url: API.DELETE_STANDARD,
      type: 'post',
      data: JSON.stringify(filter),
      contentType: 'application/json',
      datatype:'json'
    })
    .done((data) => {
      StandardAction.delStandard.completed(data);
    })
  },
  onFindStandardById(filter) {
    $.ajax({
      url: API.GET_STANDARD_DETAIL + '?id=' + filter.id,
      type: 'get',
      data: filter,
      contentType: 'application/json',
      dataType: 'json'
    })
    .done((data) => {
      this.setState({
        visible2: true,
        model: data.data
      })
    })
  },
  onUpdateStandard(filter) {
    $.ajax({
      url: API.UPDATE_STANDARD,
      type: 'put',
      data: JSON.stringify(filter),
      contentType: 'application/json',
      dataType: 'json'
    })
    .done((data) => {
      this.setState({
        visible2: false
      })
      StandardAction.updateStandard.completed(data);
    })
  }
})
